package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创建日期: 2022/05/05 18:00
 *
 * @author 孙豪骏
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;


    public List<AdminListVoBean> queryAdminListInList(String order, String sort, String username) {
        return adminMapper.queryAdminListInList(order, sort, username);

        //下面一种是优化的办法
//
//        AdminExample adminExample = new AdminExample();
//        adminExample.setOrderByClause("id" + " " +"asc");
//
//        AdminExample.Criteria criteria = adminExample.createCriteria();
//        if (username != null && !"".equals(username)) {
//            criteria.andUsernameLike(username);
//        }
//
//        List<Admin> admins = adminMapper.selectByExample(adminExample);
//        System.out.println(admins);
//        return null;
    }


    @Override
    public List<MarketRole> queryMarketRoleList() {
        return adminMapper.queryMarketRoleList();

    }

    @Override
    public void insertAdmin(AdminListVoBean admin) {
        adminMapper.insertAdminByAdminListVo(admin);
    }

    //下面内置的sql语句优先根据id查询,id为空根据名字查询
    @Override
    public AdminListVoBean queryAdmin(AdminListVoBean admin) {

        return adminMapper.queryAdminByAdminListVo(admin);
    }

    @Override
    public void updateAdmin(AdminListVoBean admin) {

        adminMapper.updateByAdminListVo(admin);
    }

    @Override
    public void deleteAdmin(AdminListVoBean admin) {
        adminMapper.deleteAdmin(admin);
    }

    @Override
    public List<Admin> queryAdminList(BaseParam baseParam) {
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedNotEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        System.out.println(admins);
        return admins;
    }

    @Override
    public List<Role> queryRoleList(BaseParam baseParam, String name) {
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedNotEqualTo(true);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return roles;
    }

    @Override
    public void updateRole(Role role) {
        System.out.println(role);
        roleMapper.updateByPrimaryKey(role);

    }

    @Override
    public void deleteRole(Role role) {
        roleMapper.deleteByPrimaryKey(role.getId());
    }

    @Override
    public void insertRole(Role role) {
        roleMapper.insert(role);
    }
}